# -*- coding: UTF-8 -*-
"""
    @Author:YTQ
    @Time: 2022/11/28 10:17
    Description:
    
"""
import os

from config import *
import logging
import matplotlib.pyplot as plt
from sklearn.metrics import classification_report


# 数据集统计
def count_text_len():
    text_len = []
    with open(TRAIN_DATA_PATH, encoding=ENCODING) as f:
        for line in f.readlines():
            text, _ = line.split('\t')
            text_len.append(len(text))
    plt.hist(text_len)
    plt.show()
    print(max(text_len))


# 下载bert模型
def download_bert_module(address):
    from transformers import AutoModel
    # tokenizer = AutoTokenizer.from_pretrained("bert-base-chinese")
    # model = AutoModelForMaskedLM.from_pretrained("bert-base-chinese")
    AutoModel.from_pretrained('bert-base-chinese', mirror='tuna', cache_dir=address)


# loading bert module
def load_bert_module(module_path):
    from transformers import BertTokenizer, BertModel
    tokenizer = BertTokenizer.from_pretrained(module_path)
    module = BertModel.from_pretrained(module_path)
    return module, tokenizer


# 评估
def get_label():
    with open(LABEL_PATH, encoding=ENCODING) as f:
        labels = [''.join(line.split()) for line in f.readlines()]
        return labels, {v: k for k, v in enumerate(labels)}


def evaluate(pred, true, target_names=None, output_dic=False):
    return classification_report(
        true,
        pred,
        target_names=target_names,
        output_dict=output_dic,
        zero_division=0
    )


# save csv
def save_to_csv(save_path, data, true=False):
    folder_create(save_path)
    file_create(save_path)
    import pandas as pd
    dataframe = pd.DataFrame(data)
    #
    encode = 'UTF-8'
    if true:
        encode = 'utf-8-sig'
    dataframe.to_csv(save_path, index=False, sep=',', mode='a', encoding=encode)
    logging.info("save data to csv success ======>>>>>>>")


# 文件夹创建
def folder_create(save_path):
    dir_path = os.path.dirname(save_path)
    if not os.path.exists(dir_path):
        os.makedirs(dir_path)


# 文件创建
def file_create(save_path):
    if not os.path.exists(save_path):
        with open(save_path, 'w', encoding='UTF-8') as f:
            f.write('')


def csv_get(path, encode):
    if not os.path.exists(path):
        logging.error("文件路径不存在")
        return None
    import pandas as pd
    data = pd.read_csv(path, sep=',', encoding=encode)
    return data
